const path = require('path');
const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const PUBLICPATH = '/';
const PORT = '9090';
const ENV = process.env.NODE_ENV || 'development';

const options = {
    publicPath: PUBLICPATH,
    loaders: {
        styles: ['style-loader', 'css-loader', 'postcss-loader'],
        imageAssets: 'url-loader?limit=6000&name=[path][name].[ext]?[hash:8]',
        iconFonts: [{
            loader: 'url-loader',
            query: {
                limit: 10000,
                name: '[path][name].[ext]?[hash:8]'
            }
        }]
    },
    globals: {
        'process.env': {
            'NODE_ENV': JSON.stringify(ENV)
        }
    },
    beforePlugins: [
        new webpack.HotModuleReplacementPlugin()
    ]
};

module.exports = function (args) {
    options.dirname = args.dirname;
    options.env = ENV;
    options.isDev = args.isDev;
    return webpackMerge(require('./webpack.base')(options), {
        devtool: 'source-map',
        devServer: {
            contentBase: path.join(args.dirname, 'dist'),
            historyApiFallback: true,
            inline: true,
            hot: true,
            port: PORT,
            host: 'localhost',
            proxy: {
                '/': {
                    bypass: function (req, res, proxyOptions) {
                        console.log('Skipping proxy for browser request.');
                        return `${PUBLICPATH}index.html`;
                    }
                }
            }
        },
        plugins: []
    });
};
